package com.lxb.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lxb.commonutils.R;
import com.lxb.commonutils.vo.EduCourseVo;
import com.lxb.eduservice.dto.CourseInfoFormDto;
import com.lxb.eduservice.dto.CourseQueryDto;
import com.lxb.eduservice.entity.EduCourse;
import com.lxb.eduservice.service.EduCourseService;
import com.lxb.eduservice.vo.course.CoursePublishVo;
import com.lxb.eduservice.vo.front.CourseWebVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 课程管理
 *
 * @author lixuebin
 * @since 2022-11-06
 */
@Api(description = "课程管理")
@RestController
@RequestMapping("/eduservice/course")
//@CrossOrigin
public class EduCourseController {

    @Autowired
    private EduCourseService courseService;

    /**
     * 新增课程
     *
     * @param courseInfoFormDto
     * @return
     */
    @ApiOperation(value = "新增课程")
    @PostMapping("/saveCourseInfo")
    public R saveCourseInfo(
            @ApiParam(name = "CourseInfoForm", value = "课程基本信息", required = true)
            @RequestBody CourseInfoFormDto courseInfoFormDto) {
        //保存提交表单信息
        String courseId = courseService.saveCourseInfo(courseInfoFormDto);
        if (!StringUtils.isEmpty(courseId)) {
            return R.ok().data("courseId", courseId);
        } else {
            return R.error().message("保存失败");
        }
    }

    /**
     * 根据id查询课程
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "根据课程id查询课程信息,")
    @GetMapping("/getCourseInfoById/{id}")
    public R getCourseById(
            @ApiParam(name = "id", value = "课程id", required = true)
            @PathVariable("id") String id) {

        CourseInfoFormDto courseInfoFormDto = courseService.getCourseById(id);
        return R.ok().data("courseInfoFormDto", courseInfoFormDto);
    }

    /**
     * 根据id查询课程信息[订单接口]
     *
     * @param courseId
     * @return
     */
    @ApiOperation(value = "根据课程id查询课程信息,")
    @PostMapping("/getCourseInfoVO/{courseId}")
    public EduCourseVo getCourseInfoVO(
            @ApiParam(name = "id", value = "课程id", required = true)
            @PathVariable("courseId") String courseId) {

        CourseWebVo courseWebVo = courseService.getBaseCourseInfo(courseId);
        EduCourseVo courseVo = new EduCourseVo();

        BeanUtils.copyProperties(courseWebVo,courseVo);
        return courseVo;
    }

    /**
     * 修改课程基本信息
     *
     * @param courseInfoFormDto
     * @return
     */
    @ApiOperation(value = "修改课程基本信息,")
    @PostMapping("/updateCourseInfo")
    public R updateCourseInfo(
            @ApiParam(name = "courseInfoFormDto", value = "封装课程表单提交基本信息", required = true)
            @RequestBody CourseInfoFormDto courseInfoFormDto) {

        courseService.updateCourseInfo(courseInfoFormDto);
        return R.ok();
    }

    /**
     * 根据ID获取课程发布信息
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "根据ID获取课程发布信息")
    @GetMapping("/coursePublishInfo/{id}")
    public R coursePublishInfoById(
            @ApiParam(name = "id", value = "课程id", required = true)
            @PathVariable String id) {

        CoursePublishVo publishVo = courseService.getCoursePublishById(id);
        return R.ok().data("publishVo", publishVo);
    }

    @ApiOperation(value = "根据id发布课程")
    @PutMapping("/publishCourse/{id}")
    public R publishCourseById(
            @ApiParam(name = "id", value = "课程id", required = true)
            @PathVariable String id) {
        boolean b = courseService.publishCourseById(id);
        return b ? R.ok().message("发布课程成功") : R.error().message("发布课程失败");
    }

    @ApiOperation(value = "分页查询课程方法")
    @GetMapping("/{current}/{limit}")
    public R pageListCourse(
            @ApiParam(name = "current", value = "当前页", required = true)
            @PathVariable long current,
            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable long limit) {
        //TODO 1.加多封装查询条件

        //1.分页参数 当前页+每页条数
        Page<EduCourse> eduCoursePage = new Page<>(current, limit);

        //2.实现分页
        //调用方法时,底层把分页的全部数据封装到eduCoursePage对象中
        courseService.page(eduCoursePage, null);


        //3.从分页对象中获取分页数据
        //总记录数
        long total = eduCoursePage.getTotal();
        //数据集合
        List<EduCourse> records = eduCoursePage.getRecords();

        return R.ok().data("total", total).data("records", records);
    }

    @ApiOperation(value = "多条件查询 + 分页查询课程")
    @PostMapping("/pageListCourseCondition/{current}/{limit}")
    public R pageListCourseCondition(
            @ApiParam(name = "current", value = "当前页", required = true)
            @PathVariable long current,
            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable long limit,
            @ApiParam(name = "courseQueryDto", value = "封装课程列表查询对象", required = false)
            @RequestBody(required = false) CourseQueryDto courseQueryDto) {

        String title = courseQueryDto.getTitle();
        String teacherId = courseQueryDto.getTeacherId();
        String subjectParentId = courseQueryDto.getSubjectParentId();
        String subjectId = courseQueryDto.getSubjectId();

        //1.分页参数 当前页+每页条数
        Page<EduCourse> eduCoursePage = new Page<>(current, limit);

        //2.多条件查询
        QueryWrapper<EduCourse> queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(title)) {
            queryWrapper.like("title", courseQueryDto.getTitle());
        }
        if (!StringUtils.isEmpty(teacherId)) {
            queryWrapper.eq("teacher_id", courseQueryDto.getTeacherId());
        }
        if (!StringUtils.isEmpty(subjectParentId)) {
            queryWrapper.like("subject_parent_id", courseQueryDto.getSubjectParentId());
        }
        if (!StringUtils.isEmpty(subjectId)) {
            queryWrapper.like("subject_id", courseQueryDto.getSubjectId());
        }

        //3.实现分页
        //调用方法时,底层把分页的全部数据封装到eduCoursePage对象中
        courseService.page(eduCoursePage, queryWrapper);


        //4.从分页对象中获取分页数据
        //总记录数
        long total = eduCoursePage.getTotal();
        //数据集合
        List<EduCourse> records = eduCoursePage.getRecords();

        return R.ok().data("total", total).data("records", records);
    }

    @ApiOperation(value = "根据id删除课程")
    @DeleteMapping("/{courseId}")
    public R deleteCourseId(
            @ApiParam(value = "courseId" ,required = true)
            @PathVariable String courseId){

        courseService.removeCourseById(courseId);
        return R.ok();
    }
}

